﻿using System.Linq;
using System.Data.Linq;
using UniMana.Data;

namespace UniMana.Win.Training
{
    public static class AutoCreateStudentID
    {
        public static string Create(this Table<Training_Student_Class> nTraining_Student_Class,
            int m_iMajorKey,
            int m_iTrainingTypeKey,
            int m_iCourseKey,
            int m_iLevelKey,
            int m_iClassKey,
            string m_strID,
            string m_strIDLength)
        {
            string m_strIDCurent = "";
            string m_prefixFirst = "";
            string m_prefixLast = "";
            int m_iIDLength = m_strIDLength.Length;
            var id = (from t in nTraining_Student_Class
                      where
                        t.Training_ClassKey == m_iClassKey
                      where t.Catalogue_MajorKey == m_iMajorKey
                      where t.Catalogue_TrainingTypeKey == m_iTrainingTypeKey
                      where t.Catalogue_CourseKey == m_iCourseKey
                      where t.Catalogue_LevelKey == m_iLevelKey
                      orderby
                        t.ID descending
                      select new
                      {
                          t.ID
                      }).Take(1).FirstOrDefault();
            try
            {
                m_strIDCurent = id.ID;
                m_prefixLast = m_strIDCurent.Substring(m_strIDCurent.Length - m_iIDLength, m_iIDLength);
                m_prefixFirst = m_strIDCurent.Substring(0, m_strIDCurent.Length - m_iIDLength);
                int _d = int.Parse(m_prefixLast);
                _d = _d + 1;
                m_prefixLast = _d.ToString();
                if (m_prefixLast.Length < m_iIDLength)
                {
                    for (int i = m_prefixLast.Length; i < m_iIDLength; i++)
                    {
                        m_prefixLast = "0" + m_prefixLast;
                    }
                }
                m_strIDCurent = m_prefixFirst + m_prefixLast;
            }
            catch
            {
                m_prefixFirst = m_strID;
                m_prefixLast = "1";
                if (m_prefixLast.Length < m_iIDLength)
                {
                    for (int i = m_prefixLast.Length; i < m_iIDLength; i++)
                    {
                        m_prefixLast = "0" + m_prefixLast;
                    }
                }
                m_strIDCurent = m_prefixFirst + m_prefixLast;
            }
            return m_strIDCurent;
        }

        public static string CreateIDWithCourseMajor(int m_iCourseKey,
            int m_iMajorKey)
        {
            string IDFinish = "";
            string IDFirst = "";
            string IDLast = "";
            int _iYear = 0;
            int _iCourse = 0;
            string _strMajor = "";
            Catalogue_CourseInfo courseinfo = new Catalogue_CourseInfo(m_iCourseKey);
            Catalogue_MajorInfo majorinfo = new Catalogue_MajorInfo(m_iMajorKey);
            // Xử lý tiếp đầu ngữ
            string[] arrayCourse = courseinfo.Properties.Name.ToString().Split(' ');
            string asdf = arrayCourse[3].ToString();
            _iYear = int.Parse(arrayCourse[3].ToString().Substring(2, 2));
            _iCourse = int.Parse(arrayCourse[1].ToString());
            _strMajor = majorinfo.Properties.ID.ToString();
            IDFirst = _iYear.ToString() + _iCourse.ToString() + _strMajor;
            // Xử lý mã sinh viên tồn tại cao nhất
            DBMain m_data = new DBMain();
            string m_strIDMax = m_data.m_DCReadOnly.Training_Student_Classes.ReturnStudentIDMax(m_iMajorKey, m_iCourseKey, IDFirst);

            if (m_iCourseKey >= 5)
            {
                IDLast = m_strIDMax.PadRight(4);
            }
            else
            {
                IDLast = m_strIDMax.PadRight(3);
            }
            int m_iIDLast = int.Parse(IDLast);
            IDLast = (m_iIDLast + 1).ToString();
            IDFinish = IDFirst + IDLast;
            return IDFinish;
        }
    }
}
